<template>
  <div class="register-container">
    <div class="register-box">
      <h1 class="register-title">数据可视化平台注册</h1>
      <form class="register-form" @submit.prevent="register">
        <div class="form-group">
          <label for="username">用户名：</label>
          <div class="input-wrapper">
            <input 
              type="text" 
              id="username" 
              v-model="username" 
              placeholder="请输入用户名"
              autocomplete="username"
            >
            <div class="input-glow"></div>
          </div>
        </div>
        <div class="form-group">
          <label for="email">邮箱：</label>
          <div class="input-wrapper">
            <input 
              type="email" 
              id="email" 
              v-model="email" 
              placeholder="请输入邮箱"
              autocomplete="email"
            >
            <div class="input-glow"></div>
          </div>
        </div>
        <div class="form-group">
          <label for="password">密码：</label>
          <div class="input-wrapper">
            <input 
              type="password" 
              id="password" 
              v-model="password" 
              placeholder="请输入密码"
              autocomplete="new-password"
            >
            <div class="input-glow"></div>
          </div>
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码：</label>
          <div class="input-wrapper">
            <input 
              type="password" 
              id="confirmPassword" 
              v-model="confirmPassword" 
              placeholder="请再次输入密码"
              autocomplete="new-password"
            >
            <div class="input-glow"></div>
          </div>
        </div>
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        <button type="submit" class="register-btn" :disabled="isLoading">{{ isLoading ? '注册中...' : '立即注册' }}</button>
        <div class="login-link">
          <span>已有账号？</span>
          <router-link to="/" class="login-btn-link">返回登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const router = useRouter();
const username = ref('');
const email = ref('');
const password = ref('');
const confirmPassword = ref('');
const errorMessage = ref('');
const isLoading = ref(false);

const register = async () => {
  // 表单验证
  if (!username.value) {
    errorMessage.value = '请输入用户名';
    return;
  }
  if (!email.value) {
    errorMessage.value = '请输入邮箱';
    return;
  }
  if (!password.value) {
    errorMessage.value = '请输入密码';
    return;
  }
  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }

  try {
    isLoading.value = true;
    errorMessage.value = '';
    
    // 发送注册请求到 Flask 后端
    const response = await axios.post('/api/register', {
      username: username.value,
      email: email.value,
      password: password.value
    });
    
    // 处理注册成功的情况
    if (response.data.success) {
      console.log('注册成功', response.data);
      
      // 可以显示成功消息或直接跳转到登录页
      alert('注册成功，请登录');
      router.push('/');
    } else {
      // 处理后端返回的错误信息
      errorMessage.value = response.data.message || '注册失败，请稍后再试';
      console.log('注册失败', response.data);
    }
  } catch (error) {
    // 处理网络错误或后端异常
    console.error('注册请求出错', error);
    errorMessage.value = '注册失败，请检查网络连接或联系管理员';
    
    // 模拟注册成功（仅用于演示，实际应用中应删除）
    setTimeout(() => {
      alert('模拟注册成功，请登录');
      router.push('/');
    }, 1000);
  } finally {
    isLoading.value = false;
  }
};
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.register-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #0a0e17;
  background-image: 
    radial-gradient(circle at 50% 50%, rgba(37, 93, 217, 0.1) 0%, transparent 80%),
    linear-gradient(to bottom, rgba(0, 30, 60, 0.4) 0%, transparent 100%),
    repeating-linear-gradient(to right, transparent, transparent 50px, rgba(6, 78, 144, 0.05) 50px, rgba(6, 78, 144, 0.05) 51px),
    repeating-linear-gradient(to bottom, transparent, transparent 50px, rgba(6, 78, 144, 0.05) 50px, rgba(6, 78, 144, 0.05) 51px);
  color: #e0f7ff;
  font-family: 'Orbitron', sans-serif;
  overflow: auto;
  position: relative;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 20% 30%, rgba(37, 93, 217, 0.15) 0%, transparent 70%),
    radial-gradient(circle at 80% 70%, rgba(37, 93, 217, 0.15) 0%, transparent 70%);
  pointer-events: none;
  z-index: 0;
}

.register-box {
  width: 100%;
  max-width: 450px;
  padding: 2.5rem;
  background-color: rgba(16, 24, 39, 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 195, 255, 0.3);
  border-radius: 8px;
  box-shadow: 0 0 30px rgba(0, 195, 255, 0.3);
  position: relative;
  z-index: 1;
  animation: pulse 4s infinite ease-in-out;
}

.register-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: linear-gradient(90deg, transparent, #00c3ff, transparent);
}

.register-title {
  margin: 0 0 2rem 0;
  text-align: center;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  background: linear-gradient(90deg, #00c3ff, #60efff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 10px rgba(0, 195, 255, 0.5);
}

.form-group {
  margin-bottom: 1.5rem;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #e0f7ff;
  font-size: 0.875rem;
  letter-spacing: 1px;
}

.input-wrapper {
  position: relative;
}

.form-group input {
  width: 100%;
  height: 2.75rem;
  padding: 0.5rem 1rem;
  background-color: rgba(0, 30, 60, 0.3);
  border: 1px solid rgba(0, 195, 255, 0.3);
  border-radius: 4px;
  font-size: 1rem;
  color: #e0f7ff;
  font-family: 'Orbitron', sans-serif;
  transition: all 0.3s ease;
}

.form-group input::placeholder {
  color: rgba(224, 247, 255, 0.5);
}

.form-group input:focus {
  outline: none;
  border-color: rgba(0, 195, 255, 0.8);
  box-shadow: 0 0 10px rgba(0, 195, 255, 0.4);
}

.input-glow {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #00c3ff, #60efff);
  transition: width 0.3s ease, left 0.3s ease;
  pointer-events: none;
}

.form-group input:focus ~ .input-glow {
  width: 100%;
  left: 0;
}

.error-message {
  color: #ff4d4f;
  background-color: rgba(255, 77, 79, 0.1);
  border: 1px solid rgba(255, 77, 79, 0.3);
  border-radius: 4px;
  padding: 0.5rem;
  margin-bottom: 1.5rem;
  font-size: 0.875rem;
  text-align: center;
  animation: errorPulse 2s infinite;
}

@keyframes errorPulse {
  0% { box-shadow: 0 0 5px rgba(255, 77, 79, 0.2); }
  50% { box-shadow: 0 0 10px rgba(255, 77, 79, 0.4); }
  100% { box-shadow: 0 0 5px rgba(255, 77, 79, 0.2); }
}

.register-btn {
  width: 100%;
  height: 2.75rem;
  padding: 0 1rem;
  background: linear-gradient(90deg, #0052cc, #00c3ff);
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 1rem;
  font-family: 'Orbitron', sans-serif;
  letter-spacing: 1px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  text-transform: uppercase;
  font-weight: 500;
}

.register-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.7s ease;
}

.register-btn:hover {
  box-shadow: 0 0 15px rgba(0, 195, 255, 0.5);
  transform: translateY(-2px);
}

.register-btn:hover::before {
  left: 100%;
}

.register-btn:active {
  transform: translateY(0);
  background: linear-gradient(90deg, #004299, #0099cc);
}

.login-link {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.875rem;
  color: #e0f7ff;
}

.login-btn-link {
  margin-left: 0.5rem;
  color: #00c3ff;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.login-btn-link::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background: linear-gradient(90deg, #00c3ff, #60efff);
  transition: width 0.3s ease;
}

.login-btn-link:hover {
  color: #60efff;
  text-shadow: 0 0 8px rgba(0, 195, 255, 0.5);
}

.login-btn-link:hover::after {
  width: 100%;
}

@keyframes pulse {
  0% { box-shadow: 0 0 15px rgba(0, 195, 255, 0.2); }
  50% { box-shadow: 0 0 30px rgba(0, 195, 255, 0.4); }
  100% { box-shadow: 0 0 15px rgba(0, 195, 255, 0.2); }
}

:deep(#app) {
  max-width: none;
  width: 100%;
  margin: 0;
  padding: 0;
}

/* 或者使用更高优先级的选择器 */
:global(#app) {
  max-width: none !important;
  width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
}
</style>